﻿//
// GNS_FriendLayer.cpp
// GameHall
//
// Created by mr.l on 16/2/2.
//
//
#include "GnsClientFrame.h"
#include "GnsClientCoreHead.h"
#include "GNS_ClientHall.h"
#include "GNS_FriendLayer.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_SceneMgr.h"
#include "GNS_HallScene.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

enum 
{
	DELETE_FRIEND = 10,
	ADD_FRIEND,	   //添加好友
	SEND_MSG,
	SEND_GIFT,   //赠送金币
	_ASSENT_BTN,  //同意添加
	_IGNORE_BTN,	//忽略邀请消息
	SERACH_BTN, //搜索
	SERACH_BTN_F, //好友列表搜索
	_SEND_MSG_OK,    //确认发送消息
	_SEND_MSG_CANCEL,   //取消发送消息
	_SEND_GOLD_OK,	   //确定赠送金币
	_SEND_GOLD_CANCEL,	 //取消赠送金币
	_DEL_FRIEND_BTN, // 删除牌友
	_DEL_FRIEND_BTN_CANCEL, // 删除牌友确认框 - 取消
	_DEL_FRIEND_BTN_OK, // 删除牌友确认框 - 确认

};

GNS_FriendLayer::GNS_FriendLayer()
{
}

GNS_FriendLayer::~GNS_FriendLayer()
{
    
}

GNS_FriendLayer* GNS_FriendLayer::createLayer()
{
    auto pGNS_FriendLayer = GNS_FriendLayer::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_FriendLayer);
    return pGNS_FriendLayer;
}

bool GNS_FriendLayer::init()
{
    if ( !GNS_YJFLayer::init() )
    {
        return false;
    }
    return true;
}
void GNS_FriendLayer::SetNodeName(std::string CsbName)
{
    m_RootNode =Loadcsb(CsbName);
    addChild(m_RootNode,11);

    auto pBackBtn = static_cast<Button*>(m_RootNode->getChildByName("Button_4"));
	pBackBtn->setTag(BTN_BACK);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	pbtnFlist = static_cast<Button*>(m_RootNode->getChildByName("Button_1"));
	pbtnFlist->setTag(BTN_F_LIST);
	pbtnFlist->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	pbtnFAdd = static_cast<Button*>(m_RootNode->getChildByName("Button_3"));
	pbtnFAdd->setTag(BTN_F_ADD);
	pbtnFAdd->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	pbtnFReq = static_cast<Button*>(m_RootNode->getChildByName("Button_2"));
	pbtnFReq->setTag(BTN_F_REQ);
	pbtnFReq->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	m_FriendListView = static_cast<ListView*>(m_RootNode->getChildByName("FriendList"));

	m_tipsPoint = static_cast<Sprite*>(m_RootNode->getChildByName("Sprite_2"));
	m_tipsPoint->setVisible(false);
	
	//------好友列表面板
	m_FriendListPanle = static_cast<Layer*>(m_RootNode->getChildByName("FriendListPanle"));
	m_serchByFriendList = static_cast<TextField*>(m_FriendListPanle->getChildByName("TextField_1"));
	m_serchBtn = static_cast<Button*> (m_FriendListPanle->getChildByName("Button_9"));
	m_serchBtn->setTag(SERACH_BTN_F);
	m_serchBtn->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	//------好友请求面板
	m_FriendReqListPanle = static_cast<Layer*>(m_RootNode->getChildByName("FriendRequestPanle"));
	//m_FriendReqListView = static_cast<ListView*>(m_FriendReqListPanle->getChildByName("ListView_1"));
	m_FriendReqListPanle->setVisible(false);

	//------添加好友面板
	m_FriendAddListPanle = static_cast<Layer*>(m_RootNode->getChildByName("FriendAddPanle"));
	//m_AddFriendListView = static_cast<ListView*>(m_FriendAddListPanle->getChildByName("ListView_3"));
	m_FriendAddListPanle->setVisible(false);
	m_serch = static_cast<TextField*>(m_FriendAddListPanle->getChildByName("TextField_1"));
	m_serchBtn = static_cast<Button*> (m_FriendAddListPanle->getChildByName("Button_9"));
	m_serchBtn->setTag(SERACH_BTN);
	m_serchBtn->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	ShowFriendList();
}

void GNS_FriendLayer::ShowFriendList()
{
	
	m_FriendListView->removeAllItems();
	bool bIsShow = (0 >= GNS_PlayerMgr::GetInstance()->getFriendList()->size()) ? true : false;
	map<long long, CMD_FriendInfo>* ii = GNS_PlayerMgr::GetInstance()->getFriendList();
	map<long long, CMD_FriendInfo>::iterator mInfo = GNS_PlayerMgr::GetInstance()->getFriendList()->begin();
	int nIndex = 0;
	for (; mInfo != GNS_PlayerMgr::GetInstance()->getFriendList()->end(); ++mInfo, ++nIndex)
	{
		if (mInfo->second.lFriendUserId == GNS_PlayerMgr::GetInstance()->getMyUserIdx())
			continue;
		createItemForFriendListView(mInfo->second.stUserSite.iState, mInfo->second.lFriendUserId, mInfo->second.szNickName, mInfo->second.lSysLogoId, mInfo->second.lGold);
	}
	m_FriendListView->setInnerContainerSize(Size(764, 98 * nIndex));
}

void GNS_FriendLayer::updateUnReadMailCount()
{
	bool bShow = false;
	map<long long, CMD_InviteFriendInfo> *pMap = GNS_PlayerMgr::GetInstance()->getInviteFriend();
	int iCount = pMap->size();
	bShow = (iCount > 0) ? true : false;
	m_tipsPoint->setVisible(bShow);
	auto tex = static_cast<Text*>(m_tipsPoint->getChildByName("Text_1"));
	if (tex)
	{
		auto str = StringUtils::format("%02d", iCount);
		tex->setString(str);
		tex->setVisible((iCount > 0) ? true : false);
	}
}

void GNS_FriendLayer::ShowAddFriendList()
{
	m_FriendListView->removeAllItems();
	bool bIsShow = (0 >= GNS_PlayerMgr::GetInstance()->getFriendList()->size()) ? true : false;
	map<long long, CMD_FriendData>::iterator mInfo = GNS_PlayerMgr::GetInstance()->getSearFriendData()->begin();
	int nIndex = 0;
	for (; mInfo != GNS_PlayerMgr::GetInstance()->getSearFriendData()->end(); ++mInfo, ++nIndex)
	{
		createItemForAddFriendListView(0, mInfo->second.lFriendUserId, mInfo->second.szNickName, mInfo->second.lSysLogoId, mInfo->second.lGold);
	}
	m_FriendListView->setInnerContainerSize(Size(764,98*nIndex));

	//删除信息
	//map<long long, CMD_FriendData> * searchResult = GNS_PlayerMgr::GetInstance()->getSearFriendData();
	//searchResult->clear();
}

void GNS_FriendLayer::ShowReqFriendList()
{
	m_FriendListView->removeAllItems();
	bool bIsShow = (0 >= GNS_PlayerMgr::GetInstance()->getFriendList()->size()) ? true : false;
	map<long long, CMD_InviteFriendInfo>::iterator mInfo = GNS_PlayerMgr::GetInstance()->getInviteFriend()->begin();
	int nIndex = 0;
	for (; mInfo != GNS_PlayerMgr::GetInstance()->getInviteFriend()->end(); ++mInfo, ++nIndex)
	{
		createItemForReqFriendListView(mInfo->second.lInvitedUserId, mInfo->second.szNickName, mInfo->second.lSysLogoId, mInfo->second.lGold);
	}
	m_FriendListView->setInnerContainerSize(Size(764, 98 * nIndex));
}

void GNS_FriendLayer::createItemForFriendListView(const int &iState, long long llUserID, const char *pcNickName, long long llSyslogoid, long long llGold)
{
	auto itemList = Button::create("rankingcell.png", "rankingcell.png");
	itemList->ignoreAnchorPointForPosition(false);
	itemList->setAnchorPoint(Vec2(0, 0));
	m_FriendListView->addChild(itemList);

	itemList->setTag(llUserID);

	auto RankSp = Sprite::create("1.png");
	RankSp->setPosition(Vec2(27.0f, 45.0f));
	itemList->addChild(RankSp);

	auto headBg = Sprite::create("Pictureframe.png");
	headBg->setPosition(Vec2(84, 45.00));
	itemList->addChild(headBg);

	auto pstrPath = StringUtils::format("face_%lld.png", llSyslogoid);
	auto headP = Sprite::create(pstrPath);	 //显示头像
	headP->setScale(0.8);
	headP->setPosition(Vec2(84, 45.00));
	itemList->addChild(headP);

	auto nameText = Text::create();	  //显示昵称
	nameText->ignoreAnchorPointForPosition(true);
	nameText->setPosition(Vec2(128, 35));
	nameText->setFontSize(20);
	nameText->setString(GTOU(pcNickName));
	itemList->addChild(nameText);

	auto idMark = Text::create();	  //id标志
	idMark->ignoreAnchorPointForPosition(true);
	idMark->setPosition(Vec2(265, 35));
	idMark->setFontSize(20);
	idMark->setString("ID:");
	itemList->addChild(idMark);

	auto idNum = Text::create();	  //显示ID
	idNum->ignoreAnchorPointForPosition(true);
	idNum->setPosition(Vec2(290, 35));
	idNum->setFontSize(20);
	auto strID = StringUtils::format("%ld", llUserID);
	idNum->setString(GTOU(strID.c_str()));
	itemList->addChild(idNum);

	/*auto goldT = Sprite::create("friendscion.png");	//金币标识
	goldT->setPosition(Vec2(143.62, 30));
	itemList->addChild(goldT);

	auto goldText = Text::create();	   //显示金币值
	goldText->ignoreAnchorPointForPosition(true);
	goldText->setFontSize(20);
	goldText->setPosition(Vec2(170, 20.50));
	auto str = StringUtils::format("%ld", llGold);
	goldText->setString(GTOU(str.c_str()));
	itemList->addChild(goldText);*/

	auto btnSendMsg = Button::create("send message_button.png", "send message_button.png");
	btnSendMsg->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	btnSendMsg->setTag(SEND_MSG);
	btnSendMsg->setPosition(Vec2(413, 50));
	itemList->addChild(btnSendMsg);

	auto btnSendGift = Button::create("Sharegift_button.png", "Sharegift_button.png");
	btnSendGift->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	btnSendGift->setTag(SEND_GIFT);
	btnSendGift->setPosition(Vec2(552, 50));
	itemList->addChild(btnSendGift);

	auto btndeleteF = Button::create("deletefriends_button.png", "deletefriends_button.png");
	btndeleteF->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	btndeleteF->setTag(_DEL_FRIEND_BTN);
	btndeleteF->setPosition(Vec2(689, 50));
	itemList->addChild(btndeleteF);
	return;
}
 //好友列表的item
void GNS_FriendLayer::createItemForAddFriendListView(const int &iState, long long llUserID, const char *pcNickName, long long llSyslogoid, long long llGold)
{
	auto itemList = Button::create("rankingcell.png", "rankingcell.png");
	itemList->ignoreAnchorPointForPosition(false);
	itemList->setAnchorPoint(Vec2(0, 0));
	m_FriendListView->addChild(itemList);

	itemList->setTag(llUserID);

	auto RankSp = Sprite::create("1.png");
	RankSp->setPosition(Vec2(27.0f, 45.0f));
	itemList->addChild(RankSp);

	auto headBg = Sprite::create("Pictureframe.png");
	headBg->setPosition(Vec2(84, 45.00));
	itemList->addChild(headBg);

	auto pstrPath = StringUtils::format("face_%lld.png", llSyslogoid);
	auto headP = Sprite::create(pstrPath);	 //显示头像
	headP->setScale(0.8);
	headP->setPosition(Vec2(84, 45.00));
	itemList->addChild(headP);

	auto nameText = Text::create();	  //显示昵称
	nameText->ignoreAnchorPointForPosition(true);
	nameText->setPosition(Vec2(128, 35));
	nameText->setFontSize(20);
	nameText->setString(GTOU(pcNickName));
	itemList->addChild(nameText);

	auto idMark = Text::create();	  //id标志
	idMark->ignoreAnchorPointForPosition(true);
	idMark->setPosition(Vec2(265, 35));
	idMark->setFontSize(20);
	idMark->setString("ID:");
	itemList->addChild(idMark);

	auto idNum = Text::create();	  //显示ID
	idNum->ignoreAnchorPointForPosition(true);
	idNum->setPosition(Vec2(290, 35));
	idNum->setFontSize(20);
	auto strID = StringUtils::format("%ld", llUserID);
	idNum->setString(GTOU(strID.c_str()));
	itemList->addChild(idNum);

	/*auto goldT = Sprite::create("friendscion.png");	//金币标识
	goldT->setPosition(Vec2(143.62, 30));
	itemList->addChild(goldT);

	auto goldText = Text::create();	   //显示金币值
	goldText->ignoreAnchorPointForPosition(true);
	goldText->setFontSize(20);
	goldText->setPosition(Vec2(170, 20.50));
	auto str = StringUtils::format("%ld", llGold);
	goldText->setString(GTOU(str.c_str()));
	itemList->addChild(goldText);*/

	Button* addFriend = Button::create("addfriend_button.png","f1.png","addfriend_button.png");
	addFriend->setTag(ADD_FRIEND);
	addFriend->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	addFriend->setPosition(Vec2(652, 50));
	itemList->addChild(addFriend);
	return;
}

void GNS_FriendLayer::createItemForReqFriendListView( long long llUserID, const char *pcNickName, long long llSyslogoid, long long llGold)
{
	auto itemList = Button::create("rankingcell.png", "rankingcell.png");
	itemList->ignoreAnchorPointForPosition(false);
	itemList->setAnchorPoint(Vec2(0, 0));
	m_FriendListView->addChild(itemList);

	itemList->setTag(llUserID);

	auto RankSp = Sprite::create("1.png");
	RankSp->setPosition(Vec2(27.0f, 45.0f));
	itemList->addChild(RankSp);

	auto headBg = Sprite::create("Pictureframe.png");
	headBg->setPosition(Vec2(84, 45.00));
	itemList->addChild(headBg);

	auto pstrPath = StringUtils::format("face_%lld.png", llSyslogoid);
	auto headP = Sprite::create(pstrPath);	 //显示头像
	headP->setScale(0.8);
	headP->setPosition(Vec2(84, 45.00));
	itemList->addChild(headP);

	auto nameText = Text::create();	  //显示昵称
	nameText->ignoreAnchorPointForPosition(true);
	//nameText->setPosition(Vec2(128.23, 58.15));
	nameText->setPosition(Vec2(128, 35));
	nameText->setFontSize(20);
	nameText->setString(GTOU(pcNickName));
	itemList->addChild(nameText);

	auto idMark = Text::create();	  //id标志
	idMark->ignoreAnchorPointForPosition(true);
	idMark->setPosition(Vec2(265, 35));
	//idMark->setPosition(Vec2(263.67, 58.15));
	idMark->setFontSize(20);
	idMark->setString("ID:");
	itemList->addChild(idMark);

	auto idNum = Text::create();	  //显示ID
	idNum->ignoreAnchorPointForPosition(true);
	//idNum->setPosition(Vec2(290, 58.15));
	idNum->setPosition(Vec2(290, 35));
	idNum->setFontSize(20);
	auto strID = StringUtils::format("%ld", llUserID);
	idNum->setString(GTOU(strID.c_str()));
	itemList->addChild(idNum);

	/*auto goldT = Sprite::create("friendscion.png");	//金币标识
	goldT->setPosition(Vec2(143.62, 30));
	itemList->addChild(goldT);

	auto goldText = Text::create();	   //显示金币值
	goldText->ignoreAnchorPointForPosition(true);
	goldText->setFontSize(20);
	goldText->setPosition(Vec2(170, 20.50));
	auto str = StringUtils::format("%ld", llGold);
	goldText->setString(GTOU(str.c_str()));
	itemList->addChild(goldText);*/

	auto btnagree = Button::create("r4.png", "r3.png");
	btnagree->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	btnagree->setTag(_ASSENT_BTN);
	btnagree->setPosition(Vec2(550, 50));
	itemList->addChild(btnagree);

	auto btnQx = Button::create("r2.png", "r1.png");
	btnQx->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	btnQx->setTag(_IGNORE_BTN);
	btnQx->setPosition(Vec2(690, 50));
	itemList->addChild(btnQx);

	return;
}

void GNS_FriendLayer::searchFriend(int tag)
{
	map<long long, CMD_FriendInfo> *pMap = GNS_PlayerMgr::GetInstance()->getFriendList();
	auto ite = pMap->find(tag);
	if (ite!=pMap->end())
	{
		m_FriendListView->removeAllChildren();
		createItemForFriendListView(ite->second.stUserSite.iState,ite->second.lFriendUserId,ite->second.szNickName,ite->second.lSysLogoId,ite->second.lGold);
	}
	else
	{
		ShowCommonTips("没有此好友!");
	}
}


void GNS_FriendLayer::searchFriend(string strTag)
{
	bool bFind = false;
	int Ftag = 0;
	Ftag = atoi(strTag.c_str());

	map<long long, CMD_FriendInfo>::iterator ite = GNS_PlayerMgr::GetInstance()->getFriendList()->begin();
	for (; ite != GNS_PlayerMgr::GetInstance()->getFriendList()->end(); ++ite)
	{
		string strNickName(ite->second.szNickName);
		string::size_type idx = strNickName.find(strTag);//查找是否含有这个 strTag 子串

		if (ite->first == Ftag || idx != string::npos)
		{
			if (!bFind)
			{
				bFind = true;
				m_FriendListView->removeAllChildren();
			}
			createItemForFriendListView(ite->second.stUserSite.iState, ite->second.lFriendUserId, ite->second.szNickName, ite->second.lSysLogoId, ite->second.lGold);

		} 
	}

	if (!bFind)
	{
		ShowCommonTips("没有此好友!");
	}
}

void GNS_FriendLayer::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
		GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
		assert(pGnsClientFrame);
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
		case BTN_BACK:
        {
			Show(false);
            break;
        }
		case BTN_F_LIST:
		{
			showListViewByTag(BtnTag);
			chaneButtonState(BTN_F_LIST);
			break;
		}
		case BTN_F_ADD:
		{
			showListViewByTag(BtnTag);
			chaneButtonState(BTN_F_ADD);
			break;
		}
		case BTN_F_REQ:
		{
			showListViewByTag(BtnTag);
			chaneButtonState(BTN_F_REQ);
			break;
		}
		case SERACH_BTN_F:
		{
			CCNode *pParent = pButton->getParent();
			if (!pParent)
			{
				 CCAssert(0, "_ADD_FRIEND invalid !");
				 break;
			}
			string str = m_serchByFriendList->getString();
			if (str.length()!=0)
			{
				//int Ftag = 0;
				//Ftag = atoi(str.c_str());
				//searchFriend(Ftag);

				searchFriend(str);
			}
			break;
		}
		case SERACH_BTN:
		{
			GNS_PlayerMgr::GetInstance()->clearSearchFriendData();
			auto pEditBox = static_cast<TextField*>(m_FriendAddListPanle->getChildByName("TextField_1"));
			CMD_FindUser findUser;
			string strSearchWord = pEditBox->getString();
			memcpy(findUser.szFindValue, strSearchWord.c_str(), sizeof(findUser.szFindValue));
			pGnsClientFrame->sendMsg(HALLSERVER,MAIN_MSG_USER, SUB_CSG_SEL_USER_INFO, (char *)&findUser, sizeof(findUser));
			break;
		}
		case ADD_FRIEND:
		{
			CCNode *pParent = pButton->getParent();
			if (!pParent)
			{
			   CCAssert(0, "_ADD_FRIEND invalid !");
			   break;
			}
			CMD_InviteFriend inviteFriend;
			inviteFriend.lInvitedUserId = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
			inviteFriend.lBeInviteeUserId = pParent->getTag();  // 被增加的好友id
			GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CSG_INVITE_FRIEND, (char *)&inviteFriend, sizeof(inviteFriend));
			ShowCommonTips("发送成功");
		    break;
		}
		case _DEL_FRIEND_BTN: // 删除牌友
			{
				//弹出删除好友的提示框
				CCNode *pParent = pButton->getParent();
				if (!pParent)
				{
				  CCAssert(0, "_ADD_FRIEND invalid !");
				  break;
				}
				m_userId = pParent->getTag();
				showTipsOfdeleteFriend();
				break;
			}
		case _DEL_FRIEND_BTN_OK: // 删除牌友确认框 - 确认
			{
				CMD_DeleteFriend deleteFriend;
				deleteFriend.lDeletedUserId = m_userId;
				deleteFriend.lUserId = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
				GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_DELETE_FRIEND, (char *)&deleteFriend, sizeof(deleteFriend));

				map<long long, CMD_FriendInfo> *pMap = GNS_PlayerMgr::GetInstance()->getFriendList();
				if (pMap)
					pMap->erase(m_userId);
				break;
			}
		case _DEL_FRIEND_BTN_CANCEL: // 删除牌友确认框 - 取消
			{
				m_deleteFriendTipsWnd->Show(false);
				break;
			}
		case _ASSENT_BTN: // 同意
		case _IGNORE_BTN: // 忽略
			{
				CCNode *pParent = pButton->getParent();
				if (!pParent)
				{
				 CCAssert(0, "_ADD_FRIEND invalid !");
				 break;
				}

				CMD_InviteFriendResult inviteFriendResult;
				inviteFriendResult.lInvitedUserId = (long long)pParent->getTag();
				inviteFriendResult.lBeInviteeUserId = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
				inviteFriendResult.bResult = _ASSENT_BTN == BtnTag ? true : false;
				GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_INVITE_RESULT, (char *)&inviteFriendResult, sizeof(inviteFriendResult));
				map<long long, CMD_InviteFriendInfo> *pMap = GNS_PlayerMgr::GetInstance()->getInviteFriend();
				pMap->erase((long long)pParent->getTag());
				ShowReqFriendList();
				//同意或者忽略从新更新tip
				updateUnReadMailCount();
				GNS_HallScene* par = (GNS_HallScene*)GNS_SceneMgr::getInstance()->getSceneLayer(LAYER_HALL);
				if (par)
				{
					par->ShowFriendTips();
				}
				break;
			}
		case SEND_GIFT:
			{
				auto pParent = dynamic_cast<Button*>(pButton->getParent());
				m_userId = pParent->getTag();
				sengGold();
				break;
			}
		case _SEND_GOLD_CANCEL:
		{
			m_SendGoldWnd->Show(false);
			break;
		}
		case _SEND_GOLD_OK:	   //确定赠送金币
		{	
			string numstr = m_SendGoldWnd-> m_imputgoldNum->getString();
			if (!CCGnsOften::isNumber(numstr))
			{
				ShowCommonTips("只能输入数字！");
				return;
			}
			
			long long Chip = atoi(numstr.c_str());
			if (GNS_PlayerMgr::GetInstance()->getMyGameGold() < Chip)
			{
				ShowCommonTips("金币不足", true);
			    break;
			} 

			string szTxt = m_SendGoldWnd->getInputString();
			//分享金币
			CMD_ShareGold ShareGold;
			ShareGold.llRequestID = 1;  //现阶段写死，将来可能用到。
			ShareGold.llWantShareUserID = m_userId;
			ShareGold.llGoldNumber = Chip;
			CCGnsOften::memcpy_safe(ShareGold.szRet, 128, UTOG(szTxt.c_str()).c_str(), szTxt.length());
			ShareGold.llFishFieldUserID = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
			GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CSG_SHARE_GOLD, (char *)&ShareGold, sizeof(CMD_ShareGold));
			m_SendGoldWnd->Show(false);
			break;
		}
		case SEND_MSG:	   //发送消息按钮
		{
			auto pParent = dynamic_cast<Button*>(pButton->getParent());
			m_userId = pParent->getTag();
			map<long long, CMD_FriendInfo>::iterator mInfo = GNS_PlayerMgr::GetInstance()->getFriendList()->find(m_userId);
			if (GNS_PlayerMgr::GetInstance()->getFriendList()->end() != mInfo)
			{
				sendMsg();
			}
			break;
		}
		case _SEND_MSG_CANCEL:   //取消发送消息
			{
				m_SendMsgWnd->Show(false);
				break;
			}
		case _SEND_MSG_OK:    //确认发送消息
			{
				auto str = UTOG(m_SendMsgWnd->getWndString().c_str());
				int count = 0;
				vector<CMD_Mail> *pMailList = GNS_PlayerMgr::GetInstance()->getFriendMailList();
				vector<CMD_Mail>::reverse_iterator rIter = pMailList->rbegin();
				for (; rIter != pMailList->rend(); rIter++)
				{
				  if (rIter->llUid == GNS_PlayerMgr::GetInstance()->getMyUserIdx() || rIter->llTid == 1 /*好友的id*/)
				  {
					  count++;
				  }
				}
				if (count >= 200)
				{
				  ShowCommonTips("邮件数量大于200条，请删除邮件", true);
				  return;
				}
				string strContent;
				strContent.append(str);
				if (0 >= strContent.length())
				{
				  return;
				}
// 				else if (string::npos != strContent.find("'"))
// 				{
// 				  // 清除内容中的 ' 字符，会对存储过程有影响
// 				  while (1)
// 				  {
// 					  string::size_type tPos = strContent.find("'");
// 					  if (tPos == string::npos)
// 						  break;
// 
// 					  strContent.erase(tPos, 1);
// 				  }
// 				  pEditBox->setString(strContent.c_str());
// 				  return;
// 				}
				CMD_Mail pMail;
				pMail.iType = 1;
				pMail.llUid = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
				pMail.llTid = m_userId;
				pMail.iMailType = MAIL_FRIEND;
				memcpy(pMail.szText,str.c_str(),str.length());
				GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_MAIL, (char *)&pMail, sizeof(pMail));
				m_SendMsgWnd->Show(false);
				break;
			}
            default:
                break;
        }
    }
}

void GNS_FriendLayer::showListViewByTag(int tag)
{
	switch (tag)
	{
	case BTN_F_LIST:
	{
		ShowFriendList();
		m_FriendListPanle->setVisible(true);
		m_FriendAddListPanle->setVisible(false);
		m_FriendReqListPanle->setVisible(false);
		break;
	}
	case BTN_F_ADD:
	{
		ShowAddFriendList();
		m_FriendListPanle->setVisible(false);
		m_FriendAddListPanle->setVisible(true);
		//m_serch->setString("");
		m_FriendReqListPanle->setVisible(false);
		break;
	}
	case BTN_F_REQ:
	{
		ShowReqFriendList();
		m_FriendListPanle->setVisible(false);
		m_FriendAddListPanle->setVisible(false);
		m_FriendReqListPanle->setVisible(true);
		break;

	}
	default:
		break;
	}	
}

void GNS_FriendLayer::showTipsOfdeleteFriend(bool isShow)
{
	 if (m_deleteFriendTipsWnd==nullptr)
	 {
		 m_deleteFriendTipsWnd = GNS_YJFLayer::createLayer("MainScenes/AgreeTipLayer.csb");
		 auto btnSbumit = static_cast<Button*>(m_deleteFriendTipsWnd->m_RootNode->getChildByName("Button_1"));
		 btnSbumit->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack,this));
		 btnSbumit->setTag(_DEL_FRIEND_BTN_OK);
		 auto btncancell = static_cast<Button*>(m_deleteFriendTipsWnd->m_RootNode->getChildByName("Button_2"));
		 btncancell->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
		 btncancell->setTag(_DEL_FRIEND_BTN_CANCEL);
		 auto text = static_cast<Text*>(m_deleteFriendTipsWnd->m_RootNode->getChildByName("Text_1"));
		 text->setString("确定删除此好友?");
	 }
	 m_deleteFriendTipsWnd->Show(isShow);
}

void GNS_FriendLayer::sendMsg()
{
	if (m_SendMsgWnd==nullptr)
	{
		m_SendMsgWnd = GNS_SendMsgWnd::createLayer("MainScenes/sendMsg.csb");
		m_SendMsgWnd->setContentSize(winsize);
		m_SendMsgWnd->setAnchorPoint(Vec2(0.5f, 0.5f));
		ui::Helper::doLayout(m_SendMsgWnd);
		m_RootNode->addChild(m_SendMsgWnd, 11);
		m_SendMsgWnd->m_submitBT->setTag(_SEND_MSG_OK);
		m_SendMsgWnd->m_submitBT->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

		m_SendMsgWnd->m_cancleBT->setTag(_SEND_MSG_CANCEL);
		m_SendMsgWnd->m_cancleBT->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

	}
	m_SendMsgWnd->Show(true);
}

void GNS_FriendLayer::sengGold()
{
	if (m_SendGoldWnd == nullptr)
	{
		m_SendGoldWnd = GNS_SendGoldWnd::createLayer("MainScenes/FriendShareLayer.csb");
		m_SendGoldWnd->setContentSize(winsize);
		m_SendGoldWnd->setAnchorPoint(Vec2(0.5f, 0.5f));
		ui::Helper::doLayout(m_SendGoldWnd);
		m_RootNode->addChild(m_SendGoldWnd, 11);

		m_SendGoldWnd->m_submitBT->setTag(_SEND_GOLD_OK);
		m_SendGoldWnd->m_submitBT->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));

		m_SendGoldWnd->m_cancleBT->setTag(_SEND_GOLD_CANCEL);
		m_SendGoldWnd->m_cancleBT->addTouchEventListener(CC_CALLBACK_2(GNS_FriendLayer::BtnCallBack, this));
	}
	m_SendGoldWnd->Show(true);
}

void GNS_FriendLayer::Show(bool isShow)
{
	GNS_YJFLayer::Show(isShow);
	updateUnReadMailCount();
	ShowFriendList();
}

void GNS_FriendLayer::setButtonState()
{
	pbtnFlist->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
	pbtnFAdd->setBrightStyle(Widget::BrightStyle::NORMAL);
	pbtnFReq->setBrightStyle(Widget::BrightStyle::NORMAL);
}

void GNS_FriendLayer::chaneButtonState(int tag)
{
	pbtnFlist->setBrightStyle((tag == pbtnFlist->getTag()) ? Widget::BrightStyle::HIGHLIGHT : Widget::BrightStyle::NORMAL);
	pbtnFAdd->setBrightStyle((tag == pbtnFAdd->getTag()) ? Widget::BrightStyle::HIGHLIGHT : Widget::BrightStyle::NORMAL);
	pbtnFReq->setBrightStyle((tag == pbtnFReq->getTag()) ? Widget::BrightStyle::HIGHLIGHT : Widget::BrightStyle::NORMAL);
}

